package cn.player.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyLetterListView extends View {

	OnTouchingLetterChangedListener onTouchingLetterChangedListener;
	String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
			"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
			"Z", "#", "" };
	int choose = -1;
	int oldChoose = -1;

	Paint paint = new Paint();
	public static boolean showBkg = false;
	int screenwide = 0;
	int screenheight = 0;

	public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		screenwide = context.getResources().getDisplayMetrics().widthPixels;
		screenheight = context.getResources().getDisplayMetrics().heightPixels;
	}

	public MyLetterListView(Context context, AttributeSet attrs) {
		super(context, attrs);

		screenwide = context.getResources().getDisplayMetrics().widthPixels;
		screenheight = context.getResources().getDisplayMetrics().heightPixels;
	}

	public MyLetterListView(Context context) {
		super(context);

		screenwide = context.getResources().getDisplayMetrics().widthPixels;
		screenheight = context.getResources().getDisplayMetrics().heightPixels;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (showBkg) {
			paint.reset();
		}

		int height = getHeight();
		int width = getWidth();
		int singleHeight = height / b.length;

		for (int i = 0; i < b.length; i++) {
			paint.setColor(Color.parseColor("#4296FF"));
			paint.setTypeface(Typeface.DEFAULT_BOLD);
			paint.setAntiAlias(true);

			if (screenheight <= 320) {
				paint.setTextSize(8);
			} else if (screenheight <= 480) {
				paint.setTextSize(10);
			} else if (screenheight <= 854) {
				paint.setTextSize(18);
			} else if (screenheight <= 1280) {
				paint.setTextSize(24);
			} else if (screenheight <= 1920) {
				paint.setTextSize(34);
			}

			if (i == choose) {
				if (showBkg) {
					paint.setColor(Color.parseColor("#e00000"));
					paint.setFakeBoldText(true);
				} else {
					paint.setColor(Color.parseColor("#4296FF"));
					paint.setFakeBoldText(true);
				}
			}

			float xPos = width / 2 - paint.measureText(b[i]) / 2;
			float yPos = singleHeight * (i + 1);

			canvas.drawText(b[i], xPos, yPos, paint);

			paint.reset();
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		final int c = (int) (y / getHeight() * b.length);

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			oldChoose = choose;
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c]);
					choose = c;
				}
			}

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c]);
					choose = c;
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c]);
					choose = c;
				}
			}
			break;
		}
		return true;
	}

	public void setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	public interface OnTouchingLetterChangedListener {
		public void onTouchingLetterChanged(String s);
	}

}
